package rule_window

import (
	"file_explorer/model"

	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
)

func (window *RuleWindow) newRuleReplaceTab(onSubmit func()) *container.TabItem {
	form := widget.NewForm()

	srcEntry := widget.NewEntry()
	srcEntry.SetPlaceHolder("支持正则替换")
	dstEntry := widget.NewEntry()

	form.Append("*原内容", srcEntry)
	form.Append("新内容", dstEntry)
	form.OnSubmit = func() {
		window.tabCtx.Rules = append(window.tabCtx.Rules, model.NewReplaceRule(srcEntry.Text, dstEntry.Text))
		onSubmit()
	}

	validator := func() {
		if len(srcEntry.Text) == 0 {
			form.Disable()
			return
		}
		form.Enable()
	}
	validator()
	srcEntry.OnChanged = func(s string) {
		validator()
	}

	return container.NewTabItem("替换", form)
}
